
<!--
 * @Author: Bennty
 * @Date: 2024-07-22 15:11:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-09-19 15:00:03
 * @Description: 请填写简介
-->
<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div class="search">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
                    <el-form-item label="模糊搜索" prop="welfareName">
                        <el-input v-model="queryParams.welfareName" placeholder="请输入" clearable />
                    </el-form-item>
                    <el-form-item label="产品状态" prop="productStatus">
                        <el-select v-model="queryParams.productStatus" placeholder="请选择" clearable filterable >
                            <el-option label="已激活" value="1"></el-option>
                            <el-option label="已禁用" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="success" plain icon="Plus" @click="handleClickAdd">新增</el-button>
                    </el-col>
                </el-row>
            </template>
            <el-table :data="productList" :loading="loading">
                <el-table-column label="产品图片" align="center">
                    <template #default="scope">
                        <el-image style="height: 200px" :src="scope.row.showImage" fit="fill" /> 
                    </template>
                </el-table-column>
                <el-table-column label="产品名称" prop="welfareName" align="center"></el-table-column>
                <el-table-column label="适配星级" align="center">
                    <template #default="scope">
                        <span>{{ `${levelOption.filter(level=>level.value == scope.row.lowLevel)[0].label} ~ ${levelOption.filter(level=>level.value == scope.row.hightLevel)[0].label}` }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="产品状态" align="center">
                    <template #default="scope">
                        <span class="actived" v-if="scope.row.productStatus == '1'">已激活</span>
                        <span class="banned" v-else>已禁用</span>
                    </template>
                </el-table-column>
                <el-table-column label="库存量" prop="initialQuantity" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                        <template #default="scope">
                        <el-tooltip content="编辑" placement="top">
                            <el-button link :style="{fontSize:'20px'}" type="primary" icon="Edit" @click="handleClickEdit(scope.row)"  ></el-button>
                        </el-tooltip>
                        <el-tooltip content="激活" placement="top" v-if="scope.row.productStatus == '0'">
                            <el-button link :style="{fontSize:'20px'}" type="success" icon="Check" @click="handleClickAcitve(scope.row)"  ></el-button>
                        </el-tooltip>
                        <el-tooltip content="禁用"  placement="top" v-if="scope.row.productStatus == '1'">
                            <el-button link :style="{fontSize:'20px'}" type="warning" icon="Close" @click="handleClickAcitve(scope.row)"  ></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link :style="{fontSize:'20px'}" type="danger" icon="Delete" @click="handleClickDelete(scope.row)"></el-button>                        
                        </el-tooltip>
                        <el-tooltip content="采购申请" placement="top">
                            <el-button link :style="{fontSize:'20px'}" type="primary" icon="Goods" @click="handleClickApply(scope.row)"></el-button>                        
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <addProduct :visible="addProductVisible" @submit="handleSubmitAdd" @close="handleCancelAdd" />
        <editProduct :visible="editProductVisible" :data="welfareInfo" @submit="handleSubmitEdit" @close="handleCancelEdit" />
        <apply :visible="applyVisible" :data="welfareInfo" @submit="handleSubmitApply" @close="handleCancelApply"/>
    </div>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { getWelfareListApi, getWelfareByIdApi,deleteWelfareApi, updateWelfareStatusApi } from '@/api/sj/welfare-crm/index';
import { welfareVO } from '@/api/sj/welfare-crm/type';
import addProduct from './components/addProduct.vue';
import editProduct from './components/editProduct.vue';
import apply from './components/apply.vue';

const queryFormRef = ref<FormInstance>();
const productList = ref<welfareVO[]>([]);
const totalData = ref(0);
const loading = ref(false);
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        welfareName: undefined,
        productStatus: undefined,
        type: 2
    },
});
const { queryParams } = toRefs(data);
const addProductVisible = ref(false);
const editProductVisible = ref(false);
const applyVisible = ref(false);
const welfareInfo = ref<welfareVO>();

const levelOption = [
    {label:'无',value:'0'},
    {label:'一星',value:'1'},
    {label:'二星',value:'2'},
    {label:'三星',value:'3'},
    {label:'四星',value:'4'},
    {label:'五星',value:'5'},
];

const getWelfareList = async ()=>{
    loading.value = true;
    const {rows, total} = await getWelfareListApi(queryParams.value);
    productList.value = rows;
    totalData.value = total;
    loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    // console.log(queryParams.value);
    getWelfareList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};


/** 新增福产品 */
const handleClickAdd = ()=>{
    addProductVisible.value = true;
};

const handleSubmitAdd = (result: boolean)=>{
    if(result){
        //刷新列表
        getWelfareList();
    }
};

const handleCancelAdd = ()=>{
    addProductVisible.value = false;
};

/** 点击激活或禁用按钮 */
const handleClickAcitve = async (row:any)=>{
    if(row.productStatus == '1'){
        if(row.purchaseFlag == '1'){
            proxy?.$modal.msgWarning("该产品已存在申请订单，请处理后再禁用");
        }else{
            await proxy?.$modal.confirm('该产品状态为已激活，是否禁用?');
            const updateData = {
                id: row.id,
                productState: 0
            };
            const { code } = await updateWelfareStatusApi(updateData);
            if(code == 200){
                proxy?.$modal.msgSuccess("已禁用该产品");
                getWelfareList();
            };
        };
    }else{
        await proxy?.$modal.confirm('该产品状态为已禁用，是否激活?');
        const updateData = {
                id: row.id,
                productState: 1
        };
        const { code } = await updateWelfareStatusApi(updateData);
        if(code == 200){
            proxy?.$modal.msgSuccess("已激活该产品");
            getWelfareList();
        };
    };
};

/** 点击修改 */
const handleClickEdit = async (row:any)=>{
    const { data } = await getWelfareByIdApi(row.id);
    welfareInfo.value = data;
    editProductVisible.value = true;
};

const handleSubmitEdit = (result: boolean) => {
    if(result){
        getWelfareList();
    };
};

/** 取消修改 */
const handleCancelEdit = ()=>{
    editProductVisible.value = false;
};

/** 点击删除福利产品 */
const handleClickDelete = async(row: welfareVO)=>{
    await proxy?.$modal.confirm('是否删除该福利产品?');
    const result = await deleteWelfareApi(row.id);
    if(result.code == 200){
        proxy?.$modal.msgSuccess("已删除");
        getWelfareList();
    };
};

/** 点击申请采购 */
const handleClickApply = async (row:any)=>{
    const { data } = await getWelfareByIdApi(row.id);
    welfareInfo.value = data;
    applyVisible.value = true;
};

const handleSubmitApply = (result: boolean) => {
    if(result){
        getWelfareList();
    }
};

/**关闭采购申请 */
const handleCancelApply = ()=>{
    applyVisible.value = false;
};

onMounted(()=>{
    getWelfareList();
    
});
</script>

<style lang="scss" scoped>
.actived{
    color: yellowgreen;
}
.banned{
    color: red;
}
</style>